﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Facade.Manager;

using Domain.Model;

using DataAccess.Exceptions;

namespace Presentation.Controllers
{
    public class ProgramController : Controller
    {
        private PortfolioManager manager;

        public ProgramController()
        {
            manager = PortfolioManager.Instance;
        }

        //
        // GET: /Program/
        [Authorize]
        public ActionResult Index()
        {
            // this is not a valid url
            return View();
        }

        //
        // GET: /Program/Create
         [Authorize(Roles = "admins")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: 
        [HttpPost]
        public ActionResult Create(Program program)
        {
            try
            {
                manager.AddProgram(program);
            }
            catch (DataRepositoryException e)
            {
                // error page
            }

            return RedirectToAction("Index", "Portfolio");
        }

        // 
        // GET: /Program/View

        public ActionResult View(int id)
        {
            Program program = manager.GetProgramById(id);

            return View(program);
        }

        // 
        // GET: /Program/Edit
        [Authorize(Roles = "admins")]
        public ActionResult Edit(int id)
        {
            return View(manager.GetProgramById(id));
        }

        //
        // POST: /Program/Edit
        [HttpPost]
        public ActionResult Edit(Program program)
        {
            try
            {
                manager.UpdateProgram(program);
            }
            catch (DataRepositoryException e)
            {
                // error page
            }

            return RedirectToAction("Index", "Portfolio");
        }

        // 
        // GET: /Program/Delete
         [Authorize(Roles = "admins")]
        public ActionResult Delete(int id)
        {
            try
            {
                manager.DeleteProgram(id);

                return RedirectToAction("Index", "Portfolio");
            }
            catch (Exception e)
            {
                return View("Error");
            }
        }
    }
}
